﻿using GodSystemBLL;
using GodSystemCommon;
using GodSystemModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GodSystem.FormChildren
{
    public partial class FormUserModify : Form
    {
        public FormUserModify()
        {
            InitializeComponent();
            InitData();
        }

        // 单例模式，只能打开一个
        private static FormUserModify formUserModify = null;

        public static FormUserModify CreateInstance()
        {
            if(formUserModify == null || formUserModify.IsDisposed)
            {
                formUserModify = new FormUserModify();
            }
            return formUserModify;
        }

        private void InitData()
        {
            this.textBox_name.Text = FormUserList.usersModel.Name;
            this.textBox_department.Text = FormUserList.usersModel.Department;
            int status = FormUserList.usersModel.Status;
            int sex = FormUserList.usersModel.Sex;
            if(sex == 1)
            {
                radioButton_SexMan.Checked = true;
            }else if(sex == 0)
            {
                radioButton_sexWoman.Checked = true;
            }

            if (status == 1)
            {
                radioButton_statusOn.Checked = true;
            }
            else if (status == 0)
            {
                radioButton_statusOff.Checked = true;
            }


        }

        private void button_sure_Click(object sender, EventArgs e)
        {
            string password = textBox_password.Text.Trim();
            string rePassword = textBox_repassword.Text.Trim();
            string newPassword = "";
            string salt = FormUserList.usersModel.Salt;
            if (!string.IsNullOrEmpty(password) || !string.IsNullOrEmpty(rePassword))
            {
                if (!password.Equals(rePassword))
                {
                    MessageBox.Show("重置两次密码不一致", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if(password.Length < 5 || password.Length > 16)
                {
                    MessageBox.Show("密码长度必须是5-16位", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                newPassword = Utility.GetMd5FromString(password + salt);
            }
            string department = textBox_department.Text.Trim();
            int status = 1;
            int sex = 1;
            if (radioButton_sexWoman.Checked)
            {
                sex = 0;
            }
            if (radioButton_statusOff.Checked)
            {
                status = 0;
            }

            UsersModel usersmodel = new UsersModel();
            usersmodel.Id = FormUserList.usersModel.Id;
            usersmodel.Password = newPassword;
            usersmodel.Status = status;
            usersmodel.Sex = sex;
            usersmodel.Department = department;
            UsersBLL userBLL = new UsersBLL();
            int result = userBLL.UpdateUserInfo(usersmodel);
            if(result == 1)
            {
                MessageBox.Show("修改成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
                this.Dispose();
            }
            else
            {
                MessageBox.Show("修改失败","提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }



        }

        private void button_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }
    }
}
